﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Web.Mvc;
using System.Web.Http;

using ERPStore.Extensions;

using ERPStore.Web.Extensions;

namespace ERPStore.Web.Controllers
{
	[System.Web.Http.Description.ApiExplorerSettings(IgnoreApi = true)]
	public class AdminApiController : ApiController
	{
		private static readonly object m_Lock = new object();

		public AdminApiController(ERPStore.Services.ICacheService cacheService
			, ERPStore.Services.ICatalogService catalogService
			, ERPStore.Services.IEmailerService emailerService)
		{
			this.CacheService = cacheService;
			this.CatalogService = catalogService;
		}

		protected ERPStore.Services.ICacheService CacheService { get; set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; set; }

		[System.Web.Http.HttpGet]
		public DateTime Ping()
		{
			return DateTime.Now;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadProductCategories()
		{
			CatalogService.ReloadCategories();
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadBrands()
		{
			CatalogService.ReloadBrands();
			return true;
		}

		// [ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadSettings()
		{
			lock (m_Lock)
			{
				var settingService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ISettingsService>();
				var existingSettings = GlobalConfiguration.Configuration.Settings;

				var newSettings = settingService.GetWebSiteSettings();
				newSettings.TempPath = existingSettings.TempPath;
				newSettings.ImagePath = existingSettings.ImagePath;
				newSettings.CurrentUrl = existingSettings.CurrentUrl;
				newSettings.PhysicalPath = existingSettings.PhysicalPath;

				ERPStore.GlobalConfiguration.Configuration.Settings = newSettings;
			}
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ClearAllCache()
		{
			CacheService.ClearAll();
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadProduct(string id)
		{
			CatalogService.ReloadProduct(id);
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadProductCategory(string id)
		{
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadBrand(string id)
		{
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadUser(string id)
		{
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool ReloadCustomer(string id)
		{
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public virtual bool Shutdown()
		{
			GlobalConfiguration.Configuration.Shutdown = true;
			return true;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public bool IsTaskInProgress(string id)
		{
			var pg = CloudSoft.Workflows.ProgressReporterCollector.Get(id);
			return pg != null;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public IQueryable<CloudSoft.Workflows.ProgressReporter> GetTaskProgressList()
		{
			return CloudSoft.Workflows.ProgressReporterCollector.GetList().AsQueryable();
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public CloudSoft.Workflows.ProgressReporter GetTaskProgress(string id)
		{
			var pg = CloudSoft.Workflows.ProgressReporterCollector.Get(id);
			return pg;
		}

		[ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public bool CancelTask(string id)
		{
			var pg = CloudSoft.Workflows.ProgressReporterCollector.Get(id);
			if (pg != null)
			{
				pg.CancelRequested = true;
				return true;
			}
			return false;
		}
	}
}
